package cn.seaboot.admin.api.model;

import cn.seaboot.admin.api.bean.ApiModel;
import cn.seaboot.admin.api.bean.ApiModelField;
import cn.seaboot.commons.core.Snowflake;
import cn.seaboot.commons.core.SnowflakeFactory;
import cn.seaboot.commons.reflect.ObjectField;
import cn.seaboot.commons.reflect.ObjectType;
import io.swagger.v3.oas.annotations.media.Schema;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import javax.validation.constraints.Digits;
import javax.validation.constraints.Size;

/**
 * 对象模型扫描
 *
 * @author Mr.css
 * @version 2022-04-18 11:41
 */
@Component
public class ModelScanner {
     private final Snowflake snowflake = SnowflakeFactory.createSnowflake();

    /**
     * 获取对象模型
     * <p>
     * 只有 class 是对象类型的时候有效，如果已经存在扫描信息，则不会覆盖历史数据
     *
     * @param projectId 工程ID
     * @param clazz     对象
     */
    public ApiModel scanApiModel(String projectId, Class<?> clazz) {
        ObjectType objectType = ObjectType.of(clazz);
        if (ObjectType.isObject(objectType)) {
            // scan object
            ApiModel model = new ApiModel();
            model.setId(this.snowflake.nextId());
            model.setProjectId(projectId);
            model.setClassName(clazz.getName());
            model.setObjectType(objectType.name());

            // descriptions
            Schema schema = clazz.getAnnotation(Schema.class);
            if (schema != null) {
                model.setDescription(schema.description());
            }

            return model;
        }
        return null;
    }

    /**
     * 获取对象模型字段，这里不进行递归处理，因为全面扫描的时候，会扫描到嵌套的类
     *
     * @param model 对象模型实体
     * @param field 对象
     */
    public ApiModelField scanApiModelField(ApiModel model, ObjectField field) {

        ApiModelField modelField = new ApiModelField();

        // open-api annotation
        Schema schema = field.getAnnotation(Schema.class);
        if (schema != null) {
            if (schema.hidden()) {
                // if hidden ignore this field
                return null;
            } else {
                modelField.setExample(schema.example());
                modelField.setFieldLabel(schema.description());
            }
        }

        // hibernate-validator base rule
        NotNull notNull = field.getAnnotation(NotNull.class);
        modelField.setNullable(notNull == null);
        Size length = field.getAnnotation(Size.class);
        if (length != null) {
            modelField.setMaxLength(length.max());
        }
        Digits digits = field.getAnnotation(Digits.class);
        if (digits != null) {
            modelField.setNumericPrecision(digits.integer());
            modelField.setNumericScale(digits.fraction());
        }

        // name/class
        modelField.setProjectId(model.getProjectId());

        modelField.setModelId(model.getId());
        modelField.setClassName(model.getClassName());

        modelField.setId(this.snowflake.nextId());
        modelField.setFieldName(field.getName());

        ObjectType objectType = field.getObjectType();
        modelField.setFieldType(field.getType().toString());
        modelField.setObjectType(objectType.name());

        return modelField;
    }
}
